/**
 * 
 */
package edu.pku.sei.modeler.editparts;

import java.beans.PropertyChangeEvent;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.draw2d.IFigure;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.editpolicies.DirectEditPolicy;

import edu.pku.sei.modeler.editpolicies.UMLDiagramGraphicalNodeEditPolicy;
import edu.pku.sei.modeler.figures.ChoicePseudostateFigure;
import edu.pku.sei.modeler.figures.DeephistoryPseudostateFigure;
import edu.pku.sei.modeler.figures.EntrypointPseudostateFigure;
import edu.pku.sei.modeler.figures.ExitpointPseudostateFigure;
import edu.pku.sei.modeler.figures.ForkPseudostateFigure;
import edu.pku.sei.modeler.figures.InitialPseudostateFigure;
import edu.pku.sei.modeler.figures.JoinPseudostateFigure;
import edu.pku.sei.modeler.figures.JunctionPseudostateFigure;
import edu.pku.sei.modeler.figures.ShallowhistoryPseudostateFigure;
import edu.pku.sei.modeler.figures.TerminatePseudostateFigure;
import edu.pku.sei.modeler.model.structure.MetaNode;
import edu.pku.sei.modeler.model.structure.MetaShapeElement;
import edu.pku.sei.modeler.model.uml.BehaviorStateMachines.Pseudostate;

/**
 * @author YanYan
 * 
 */
public class PseudostateEditpart extends AbstractUMLNodeEditPart {

	public PseudostateEditpart(MetaShapeElement model) {
		super(model);
		// TODO Auto-generated constructor stub
	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see edu.pku.sei.modeler.edit.parts.BaseEditPart#createDirectEditPolicy()
	 */
	@Override
	protected DirectEditPolicy createDirectEditPolicy() {
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFigure()
	 */
	@Override
	protected IFigure createFigure() {
		Pseudostate pseudostate = this.getPseudostate();
		if (pseudostate.getKind().getValue() == 0) {
			pseudostate.setName("Initial");
			return new InitialPseudostateFigure();
		} else if (pseudostate.getKind().getValue() == 1) {
			pseudostate.setName("DeepHistory");
			return new DeephistoryPseudostateFigure();
		} else if (pseudostate.getKind().getValue() == 2) {
			pseudostate.setName("ShallowHistory");
			return new ShallowhistoryPseudostateFigure();
		} else if (pseudostate.getKind().getValue() == 3) {
			pseudostate.setName("Join");
			return new JoinPseudostateFigure();
		} else if (pseudostate.getKind().getValue() == 4) {
			pseudostate.setName("Fork");
			return new ForkPseudostateFigure();
		} else if (pseudostate.getKind().getValue() == 5) {
			pseudostate.setName("Junction");
			return new JunctionPseudostateFigure();
		} else if (pseudostate.getKind().getValue() == 6) {
			pseudostate.setName("Choice");
			return new ChoicePseudostateFigure();
		} else if (pseudostate.getKind().getValue() == 7) {
			pseudostate.setName("EntryPoint");
			return new EntrypointPseudostateFigure();
		} else if (pseudostate.getKind().getValue() == 8) {
			pseudostate.setName("ExitPoint");
			return new ExitpointPseudostateFigure();
		} else if (pseudostate.getKind().getValue() == 9) {
			pseudostate.setName("Terminate");
			return new TerminatePseudostateFigure();
		}
		return null;
	}

	protected void createEditPolicies() {
		super.createEditPolicies();
	}

	@Override
	protected IFigure getDirectEditFigure() {
		return null;
	}

	protected void handlePropertyChanged(PropertyChangeEvent event) {
		// String propertyName = event.getPropertyName();
		super.handlePropertyChanged(event);
		refreshVisuals();
	}

	private Pseudostate getPseudostate() {
		return (Pseudostate) ((MetaNode) getModel()).getModelElement();
	}

	protected void refreshVisuals() {
		super.refreshVisuals();
		MetaNode modelEle = getNode();
		if (this.getPseudostate().getKind().getValue() == 0) {
			if (modelEle.getWidth() != 18 || modelEle.getHeight() != 18) {
				modelEle.setWidth(18);
				modelEle.setHeight(18);
			}
		} else if (this.getPseudostate().getKind().getValue() == 1) {
			if (modelEle.getWidth() != 22 || modelEle.getHeight() != 22) {
				modelEle.setWidth(22);
				modelEle.setHeight(22);
			}
			// TODO
		} else if (this.getPseudostate().getKind().getValue() == 2) {
			if (modelEle.getWidth() != 22 || modelEle.getHeight() != 22) {
				modelEle.setWidth(22);
				modelEle.setHeight(22);
			}
		} else if (this.getPseudostate().getKind().getValue() == 3) {
			if (modelEle.getWidth() < modelEle.getHeight()) {
				modelEle.setWidth(9);
			} else if (modelEle.getWidth() >= modelEle.getHeight()) {
				modelEle.setHeight(9);
			}
		} else if (this.getPseudostate().getKind().getValue() == 4) {
			if (modelEle.getWidth() < modelEle.getHeight()) {
				modelEle.setWidth(9);
			} else if (modelEle.getWidth() >= modelEle.getHeight()) {
				modelEle.setHeight(9);
			}
		} else if (this.getPseudostate().getKind().getValue() == 5) {
			if (modelEle.getWidth() != 20 || modelEle.getHeight() != 20) {
				modelEle.setWidth(20);
				modelEle.setHeight(20);
			}
		} else if (this.getPseudostate().getKind().getValue() == 6) {
		} else if (this.getPseudostate().getKind().getValue() == 7) {
			if (modelEle.getWidth() != 20 || modelEle.getHeight() != 20) {
				modelEle.setWidth(20);
				modelEle.setHeight(20);
			}
		} else if (this.getPseudostate().getKind().getValue() == 8) {
			if (modelEle.getWidth() != 20 || modelEle.getHeight() != 20) {
				modelEle.setWidth(20);
				modelEle.setHeight(20);
			}
		} else if (this.getPseudostate().getKind().getValue() == 9) {
			// if (modelEle.getWidth()!=45 || modelEle.getHeight()!=24) {
			// modelEle.setWidth(45);
			// modelEle.setHeight(24);
			// }
		}
	}

}
